import { defineStore } from 'pinia'

export const userStore = defineStore('user',{
    //存储全局变量
    state:()=>({
        user:{},         //  用户信息
        loggedIn:false,  //  是否登录
        favoriteMap: {} // 初始化为普通对象
    }),
    //同步修改状态信息
    actions:{
        addFavorite(bookId) {
            this.favoriteMap[bookId] = true;
        },
        removeFavorite(bookId) {
            delete this.favoriteMap[bookId]; // 建议使用 delete 而非设为 false
        },
        updateUser(user){
            this.user = user;
        },
        updateLoginStatus(status){
            this.loggedIn = status;
        }
    },
    persist: {
        enable: true,
        storage: localStorage,
    }
})
